package com.neuedu.service.impl;

import com.neuedu.common.JSONData;
import com.neuedu.dao.CategoryMapper;
import com.neuedu.pojo.Category;
import com.neuedu.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public JSONData<String> addCategory(String categoryName, Integer parentId) {
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());

        int count = categoryMapper.insert(category);
        if(count>0){
            return JSONData.buildSuccess("分类信息，添加成功");
        }else{
            return JSONData.buildError("添加失败！");
        }

    }

    public JSONData<String> updateCategoryById(String categoryName, Integer id) {
        Category category = new Category();
        category.setId(id);
        category.setName(categoryName);
        category.setUpdateTime(new Date());

        int count = categoryMapper.updateCategoryById(category);
        if(count>0){
            return JSONData.buildSuccess("分类信息修改成功");
        }else{
            return JSONData.buildError("修改失败");
        }
    }

    public JSONData<List<Category>> selAllChildrenByCategoryId(Integer categoryId) {
        List<Category> categories = categoryMapper.selAllChildrenByCategoryId(categoryId);
        if(categories == null){
            return JSONData.buildError("操作失败");
        }else{
            return JSONData.buildSuccess(categories);
        }
    }

    public JSONData<Category> selAllChildrenAndDeepByCategoryId(Integer categoryId) {
        Category category = findAllChildren(categoryId);
        return JSONData.buildSuccess(category);
    }

    /**
     * 递归算符，获取某个节点下所有的子节点
     */
    private Category findAllChildren(Integer categoryId){

        //1、获取当前id的对象
        Category category = categoryMapper.selectByPrimaryKey(categoryId);

        //2、获取当前对象下的子节点
        List<Category> categories = categoryMapper.selAllChildrenByCategoryId(categoryId);

        //3、将所有的子节点放到当前的对象中
        if(categories!=null && categories.size()>0){
            category.setCategories(categories);
        }

        //4、查询其子节点下是否还有子节点，如果有则继续进行查询
        for(Category category1 : categories){
            findAllChildren(category1.getId());
        }
        return category;
    }

    public JSONData<String> checkCategoryIsHavChild(Integer categoryId) {
        int count = categoryMapper.checkCategoryIsHavChild(categoryId);
        if(count>0){
            return JSONData.buildError("此节点下包含子节点，不能删除");
        }
        return JSONData.buildSuccess("此节点下不包含子节点，可以删除");
    }

    public JSONData<String> delCategoryById(Integer categoryId) {
        int count = categoryMapper.deleteByPrimaryKey(categoryId);
        if(count>0){
            return JSONData.buildSuccess("此节点删除成功");
        }
        return JSONData.buildError("此节点删除失败");
    }

}
